﻿using System;

using NUnit.Framework;

using OpenQA.Selenium;
using System.Collections.ObjectModel;

namespace mvcForum.Tests.WebUI {

	[TestFixture]
	public class M030_AnnouncementStickyAndPaging {
		private IWebDriver driver;
		private String forumName;

		#region Initialization and cleanup
		[TestFixtureSetUp]
		public void TestInitialize() {
			driver = TestHelper.GetDriver();

			forumName = Guid.NewGuid().ToString();

			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.CreateForum("Test board", "Category 1", forumName, "", 1);
			driver.SetStandardAccess("Test board", "Category 1", forumName);
			driver.DisableAntiSpam();
			driver.LogOff();
		}

		[TestFixtureTearDown]
		public void TestCleanup() {
			if (driver != null) {
				driver.Close();
			}
		}
		#endregion

		[Test]
		public void PagingTestWithAnnouncements() {
			Int32 topicsPerPage = 5;
			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.VisitURL("forumadmin/settings");
			driver.InsertText("TopicsPerPage", topicsPerPage.ToString());
			driver.FindButton("Update").Click();
			driver.LogOff();

			driver.LogOn(Constants.Users.Moderator.Email, Constants.Users.Moderator.Password);
			driver.CreateTopic(forumName, "Announcement", "First announcement", "bla bla");
			driver.CreateTopic(forumName, "Announcement", "Second announcement", "bla bla");
			driver.CreateTopic(forumName, "Sticky", "First sticky", "bla bla");
			driver.LogOff();

			driver.LogOn(Constants.Users.Regular.Email, Constants.Users.Regular.Password);
			driver.CreateTopic(forumName, "First regular", "bla bla");
			driver.CreateTopic(forumName, "Second regular", "bla bla");
			driver.CreateTopic(forumName, "Third regular", "bla bla");
			driver.CreateTopic(forumName, "Fourth regular", "bla bla");
			driver.CreateTopic(forumName, "Fifth regular", "bla bla");
			driver.CreateTopic(forumName, "Sixth regular", "bla bla");
			driver.LogOff();

			driver.GoToForum(forumName);

			ReadOnlyCollection<IWebElement> results = driver.FindElements(By.ClassName("topictitle"));
			Assert.AreEqual(topicsPerPage, results.Count, String.Format("We should have {0} topics, but got {1}", topicsPerPage, results.Count));
			if (results.Count == topicsPerPage) {
				Assert.AreEqual(true, results[0].Text.Contains("announcement") && results[1].Text.Contains("announcement"));
				Assert.AreEqual(true, results[2].Text.Contains("sticky"));

				ReadOnlyCollection<IWebElement> paginationBlocks = driver.FindElements(By.ClassName("pagination"));
				Assert.AreEqual(2, paginationBlocks[0].FindElements(By.XPath(".//a")).Count, "should find 2 links / 2 pages.");

				paginationBlocks[0].FindElements(By.XPath(".//a"))[0].Click();

				results = driver.FindElements(By.ClassName("topictitle"));
				Assert.AreEqual(topicsPerPage, results.Count, String.Format("We should have {0} topics, but got {1}", topicsPerPage, results.Count));
				if (results.Count == topicsPerPage) {
					Assert.AreEqual(true, results[0].Text.Contains("announcement") && results[1].Text.Contains("announcement"));
					Assert.AreEqual(true, !results[2].Text.Contains("sticky") & !results[2].Text.Contains("announcement"));
				}

				paginationBlocks = driver.FindElements(By.ClassName("pagination"));
				paginationBlocks[0].FindElements(By.XPath(".//a"))[1].Click();
				results = driver.FindElements(By.ClassName("topictitle"));
				Assert.AreEqual(3, results.Count, String.Format("We should have {0} topics, but got {1}", 3, results.Count));
				if (results.Count == 3) {
					Assert.AreEqual(true, results[0].Text.Contains("announcement") && results[1].Text.Contains("announcement"));
					Assert.AreEqual(true, !results[2].Text.Contains("sticky") & !results[2].Text.Contains("announcement"));
				}
			}
		}
	}
}